//
//  WCNPICCCareRecordVC.m
//  WCN
//
//  Created by 陈武琦 on 16/3/29.
//  Copyright © 2016年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNPICCPreserveRecordAddVC.h"
#import "WCNPICCPreserveRecordAddCell.h"
#import "Patient.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "WSJPickAndCustomSegment.h"
#import "WCNMulTextEditVC.h"
#import "WCInfoEditView.h"
#import "WCDatePickerView.h"
#import "NSDate+Utilities.h"
#import "WCNPICCPreserveRecordModel.h"
#import "WCButton.h"
#import "UINavigationItem+Offset.h"
#import "JCAlertView.h"
#import "WCTSetTextTool.h"
#import "WCDrawBorderView.h"
#import "WCNCareRecordDetailFooterView.h"
#import "WSJHorTableView.h"
#import "WCImageManager.h"
#import "WSJImage.h"
#import "UMMobClick/MobClick.h"
#import "Constants.h"

static NSString *cellID = @"WCNPICCPreserveRecordAddVCID";

@interface WCNPICCPreserveRecordAddVC ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, assign) WCNPICCPreserveRecordType recordType;
@property (nonatomic, strong) WCNPICCPreserveRecordModel *model;
@property (nonatomic, strong) UIButton *saveBtn;
//行标题
@property (nonatomic, copy) NSMutableArray *titlesArr;

//局部情况 下面的  标题
@property (nonatomic, copy) NSArray *lastTitlesArr;

@property (nonatomic, copy) NSMutableArray *titleKeysArr;
@property (nonatomic, copy) NSArray *lastTitleKeys;

@property (nonatomic, strong) MBProgressHUD *hud;

@property (nonatomic, assign) BOOL isDisplayLastTitles;
@property (nonatomic, assign) BOOL isDisplayBodyConditionTitles;
@property (nonatomic, copy) NSString *temperature;


@property (nonatomic,  strong) WSJHorTableView *picTableView;
@property (weak, nonatomic) UIView *footerView;
//已经上传的图片
@property (nonatomic,  strong) NSMutableArray *webImages;
@property (nonatomic,  strong) NSMutableArray *imageUrls;
@end

@implementation WCNPICCPreserveRecordAddVC

- (instancetype)initWithType:(WCNPICCPreserveRecordType)type
{
    self = [super init];
    if (self) {
        _recordType = type;
    }
        return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _model = [[WCNPICCPreserveRecordModel alloc] init];
    _isDisplayLastTitles = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.tableView registerClass:[WCNPICCPreserveRecordAddCell class] forCellReuseIdentifier:cellID];
    self.isDisplayBodyConditionTitles = YES;
    switch (self.recordType) {
        case WCNPICCPreserveRecordTypeScan:{
            self.title = @"维护记录详情";
            [self getRecordData];
        }
            break;
            
        case WCNPICCPreserveRecordTypeAdd:{
            self.title = @"新增维护记录";
            WCButton *rightBtn = [WCButton wcButtonWithType:WCButtonTypeSave];
            [rightBtn addTarget:self action:@selector(upLoadImages) forControlEvents:UIControlEventTouchUpInside];
            _saveBtn = rightBtn;
            UIView *tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 130)];
            [self loadFooterView:tableFooterView];
            self.tableView.tableFooterView = tableFooterView;
            [self.navigationItem addRightBarButtonItem:rightBtn];
            
        }
            break;
        case WCNPICCPreserveRecordTypeEdit:{
            self.title = @"修改维护记录";
            [self getRecordData];
            WCButton *rightBtn = [WCButton wcButtonWithType:WCButtonTypeSave];
            [rightBtn addTarget:self action:@selector(upLoadImages) forControlEvents:UIControlEventTouchUpInside];
            _saveBtn = rightBtn;
            [self.navigationItem addRightBarButtonItem:rightBtn];
            [self getImageWithUrls:nil];
        }
            break;
            
        default:
            break;
    }


    // Do any additional setup after loading the view.
}
- (NSMutableArray *)webImages
{
    if (!_webImages) {
        _webImages  =[NSMutableArray array];
    }
    return _webImages;
}
- (void)pageBack{
    if (self.recordType == WCNPICCPreserveRecordTypeScan) {
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }

    defineWeakSelf;
    [JCAlertView showTwoButtonsWithTitle:@"提示" Message:@"是否放弃编辑" ButtonType:0 ButtonTitle:@"放弃编辑" Click:^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    } ButtonType:1 ButtonTitle:@"继续编辑" Click:^{
        
    }];
    
}

- (NSMutableArray *)titlesArr {

    if (!_titlesArr) {
        _titlesArr = [NSMutableArray arrayWithArray:@[@"维护日期", @"更换贴膜", @"冲封管", @"更换接头", @"全身状况",  @"局部状况"]];
    }
    
    return _titlesArr;
}

- (NSArray *)lastTitlesArr {

    //@[@"是否做过B超检查", @"处理方式"], @"用药情况",
    if (!_lastTitlesArr) {
        _lastTitlesArr = @[@[@"穿刺点", @"处理方式"], @"穿刺侧手臂肿胀", @[@"皮肤问题", @"处理方式"], @[@"是否堵管", @"处理方式"], @[@"导管滑出", @"处理方式"], @[@"导管破损", @"处理方式"]];
 
    }
    return _lastTitlesArr;
}

- (NSMutableArray *)titleKeysArr {


    if (!_titleKeysArr) {
        _titleKeysArr = [NSMutableArray arrayWithArray:@[@"maintainDate",@"replacementFilm",@"flushPipe", @"replacementJoint", @"bodyCondition",@"localCondition"]];
    }

    return _titleKeysArr;
}

- (NSArray *)lastTitleKeys{
    
    //@[@"isBUltrasoundExamination", @"punctureArmSwellingMessure"], @"medicationSituation",
    if (!_lastTitleKeys) {
        _lastTitleKeys = @[@[@"puncturePoint", @"puncturePointMessure"],@"punctureArmSwelling", @[@"skinProblem", @"skinProblemMessure"], @[@"isBlockTube", @"blockTubeMessure"], @[@"catheterSkipLength", @"catheterSkipMessure"], @[@"catheterBreakage", @"catheterBreakageMessure"]];
    }
    
    return _lastTitleKeys;
}



- (void)setModel:(WCNPICCPreserveRecordModel *)model {
    if ([_model isEqual:model]) {
        
    }else {
        _model = model;
        if ([_model valueForKey:@"washingLiquid"] && ![self.titleKeysArr containsObject:@"washingLiquid"]) {
            [self.titlesArr insertObject:@"冲洗用液" atIndex:[self.titlesArr indexOfObject:@"冲封管"] + 1];
            [self.titleKeysArr insertObject:@"washingLiquid" atIndex:[self.titleKeysArr indexOfObject:@"flushPipe"] + 1];
   
        }else if ([_model valueForKey:@"nonPressureJointType"] && ![self.titleKeysArr containsObject:@"nonPressureJointType"]) {
            [self.titlesArr insertObject:@"非正压接头类型" atIndex:[self.titlesArr indexOfObject:@"更换接头"] + 1];
            [self.titleKeysArr insertObject:@"nonPressureJointType" atIndex:[self.titleKeysArr indexOfObject:@"replacementJoint"] + 1];
        }
        if ([[_model valueForKey:@"localCondition"] isEqualToString: @"异常"]) {
            [self.titlesArr addObjectsFromArray:self.lastTitlesArr];
            [self.titleKeysArr addObjectsFromArray:self.lastTitleKeys];
            
            if ([_model valueForKey:@"isBUltrasoundExamination"] && ![self.titleKeysArr containsObject:@"isBUltrasoundExamination"]) {
                [self.titlesArr insertObject:@"是否做过B超检查" atIndex:[self.titlesArr indexOfObject:@"穿刺侧手臂肿胀"] + 1];
                [self.titleKeysArr insertObject:@"isBUltrasoundExamination" atIndex:[self.titleKeysArr indexOfObject:@"punctureArmSwelling"] + 1];
    
            }else if ([_model valueForKey:@"punctureArmSwellingMessure"] && ![self.titleKeysArr containsObject:@"punctureArmSwellingMessure"]) {
                [self.titlesArr insertObject:@"处理方式" atIndex:[self.titlesArr indexOfObject:@"穿刺侧手臂肿胀"] + 1];
                [self.titleKeysArr insertObject:@"punctureArmSwellingMessure" atIndex:[self.titleKeysArr indexOfObject:@"punctureArmSwelling"] + 1];
    
            }else if ([_model valueForKey:@"medicationSituation"] && ![self.titleKeysArr containsObject:@"medicationSituation"]) {
                [self.titlesArr insertObject:@"用药情况" atIndex:[self.titlesArr indexOfObject:@"穿刺侧手臂肿胀"] + 1];
                [self.titleKeysArr insertObject:@"medicationSituation" atIndex:[self.titleKeysArr indexOfObject:@"punctureArmSwelling"] + 1];
                
            }
            
        }
        
        NSMutableArray *imageUrls = [NSMutableArray array];
        
        if (model.image1) {
            [imageUrls addObject:model.image1];
        }
        if (model.image2) {
            [imageUrls addObject:model.image2];
        }
        if (model.image3) {
            [imageUrls addObject:model.image3];
        }
        _imageUrls = imageUrls;
        
        
        switch (self.recordType) {
            case WCNPICCPreserveRecordTypeScan:{
                if (_imageUrls.count > 0) {
                    UIView *tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 130)];
                    [self loadFooterView:tableFooterView];
                    self.tableView.tableFooterView = tableFooterView;
                    
                }
            }
                break;
                
            case WCNPICCPreserveRecordTypeEdit:{
                [self getImageWithUrls:imageUrls];
            }
                break;
                
                
            default:
                break;
        }

        [self.tableView reloadData];
    }
}


#pragma mark - 获取记录内容
- (void)getRecordData{
    
    weakSelf_define;
    MBProgressHUD *hud;
    //获取问卷记录列表
    
    hud = [[WCPopupLayerManager shareMg] showSimple:[UIApplication sharedApplication].keyWindow];
    NSDictionary *param = @{@"id":@(self.piccId)};
    [[WCNetworkAccessManager sharedMg] POST:kQueryPICCPreserveRecord parameters:param success:^(NSDictionary *result){
        NSDictionary *dict = result[@"data"];
        WCNPICCPreserveRecordModel *model = [[WCNPICCPreserveRecordModel alloc] init];
        [model setValuesForKeysWithDictionary:dict];
        weakSelf.model = model;
    } failure:nil netError:nil always:^{
        [hud hide:YES];
    }];

}

//添加维护记录
- (void)upLoadPreserveRecordWithImageNames:(NSArray *)imageNames{
    
    self.saveBtn.enabled = NO;
        weakSelf_define;
    MBProgressHUD *hud;
    if (!self.hud) {
        hud = [[WCPopupLayerManager shareMg] showSimple:[UIApplication sharedApplication].keyWindow];
        self.hud = hud;
    }
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:[self.model keyValues]];
    for (int i = 0; i < imageNames.count; i++) {
        [params setObject:imageNames[i] forKey:[@"image" stringByAppendingFormat:@"%d",i+1]];
    }
    NSString *url;
    switch (self.recordType) {
            
        case WCNPICCPreserveRecordTypeAdd:{
            NSInteger nurseId =[[WCDefaultManager shareMg] nurseId];
            NSNumber *nurseNum = [NSNumber numberWithInteger:nurseId];
            [params setObject:@(self.piccPatientId) forKey:@"piccId"];
            [params setObject:nurseNum forKey:@"createBy"];
            url = kAddPICCPreserveRecord;
        }
            break;
        case WCNPICCPreserveRecordTypeEdit:{
            [params setObject:@(self.piccId) forKey:@"id"];
            url = kUpdatePICCPreserveRecord;
        }
            break;
            
        default:
            break;
    }
   
    [[WCNetworkAccessManager sharedMg] POST:url parameters:params success:^(NSDictionary *result){
        CoreSVPSuccess(result[@"data"]);
        //友盟统计
        if (weakSelf.recordType == WCNPICCPreserveRecordTypeAdd) {
            [MobClick event:AddPICCMaintenanceRecord];
        }else{
            [MobClick event:MotifyPICCMaintenanceRecord];
        }
        
        [weakSelf.navigationController popViewControllerAnimated:YES];
    } failure:nil netError:nil always:^{
        weakSelf.saveBtn.enabled = YES;
        [weakSelf.hud hide:YES];
        
    }];
}

- (BOOL)isWriteMoreOneLocalCondition {

   
    BOOL isWriteLocalCondition = NO;
    for (id obj in self.lastTitleKeys) {
        if ([obj isKindOfClass:[NSString class]]) {
            if ([self.model valueForKey:obj]) {
                isWriteLocalCondition = YES;
            }
        }else {
            for (NSString *key in obj) {
                if ([self.model valueForKey:key]) {
                    isWriteLocalCondition = YES;
                }
            }
            
        }
    }
    if (!isWriteLocalCondition) {
        
        WSJSHOW(@"请填写至少一项异常内容");
    }

    return isWriteLocalCondition;
}

- (void)setIsDisplayBodyConditionTitles:(BOOL)isDisplayBodyConditionTitles {
    _isDisplayBodyConditionTitles = isDisplayBodyConditionTitles;
    if (self.isDisplayBodyConditionTitles) {
        [self.titlesArr insertObjects:@[@"发热", @"血培养"] atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange([self.titlesArr indexOfObject:@"全身状况"] + 1, 2)]];
        [self.titleKeysArr insertObjects:@[@"feverTemperatue", @"bloodCulture"] atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange([self.titlesArr indexOfObject:@"全身状况"]  +1, 2)]];
        
    }else {
        [self.titlesArr removeObjectsInArray:@[@"发热", @"血培养"]];
        [self.titleKeysArr removeObjectsInArray:@[@"feverTemperatue", @"bloodCulture"]];
        
    }
    
    [self.tableView reloadData];
}


#pragma mark -UITableViewDataSource------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if ([self.titlesArr[section] isKindOfClass:[NSString class]]) {
        return 1;
    }else {
        
        return [self.titlesArr[section] count];
    }

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.titlesArr.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WCNPICCPreserveRecordAddCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[WCNPICCPreserveRecordAddCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    
    if ([self.titlesArr[indexPath.section] isEqual:@"局部状况"]) {
        cell.titleLabel.text = self.titlesArr[indexPath.section];
        NSString *key = self.titleKeysArr[indexPath.section];
        cell.contentText = [self.model valueForKey:key];
        if (self.isDisplayLastTitles) {
            [cell.rightIcon setImage:[UIImage imageNamed:@"Arrow_Down"]];
        }else{
        
            [cell.rightIcon setImage:[UIImage imageNamed:@"Arrow_Right"]];
        }
    }else if ([self.titlesArr[indexPath.section] isEqual:@"全身状况"]){
        cell.titleLabel.text = self.titlesArr[indexPath.section];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (self.isDisplayBodyConditionTitles) {
            [cell.rightIcon setImage:[UIImage imageNamed:@"Arrow_Down"]];
        }else {
            [cell.rightIcon setImage:[UIImage imageNamed:@"Arrow_Right"]];
        
        }
    
    }else {
        [cell.rightIcon setImage:[UIImage imageNamed:@"Arrow_Right"]];
        if ([self.titlesArr[indexPath.section] isKindOfClass:[NSString class]]) {
            cell.titleLabel.text = self.titlesArr[indexPath.section];
            NSString *key = self.titleKeysArr[indexPath.section];
                cell.contentText = [self.model valueForKey:key];
        }else {
            if ([self.titlesArr[indexPath.section][indexPath.row] isEqual:@"导管滑出"]){
                cell.titleLabel.text = self.titlesArr[indexPath.section][0];
                NSString *key = self.titleKeysArr[indexPath.section][indexPath.row];
                if ([self.model valueForKey:key]) {
                    
                    cell.contentText = [NSString stringWithFormat:@"%@  CM",[self.model valueForKey:key]];
                }else {
                    cell.contentText = [@"" stringByAppendingString:@"  CM"];
                }
            }else if ([self.titlesArr[indexPath.section][indexPath.row] isEqual:@"导管破损"]){
                cell.titleLabel.text = self.titlesArr[indexPath.section][indexPath.row];
                NSString *key = self.titleKeysArr[indexPath.section][indexPath.row];
                if ([self.model valueForKey:key]) {
                    
                    cell.contentText = [[self.model valueForKey:key] stringByAppendingString:@"  位置"];
                }else {
                    cell.contentText = [@"" stringByAppendingString:@"  位置"];
                    
                }
            }else {
            
                cell.titleLabel.text = self.titlesArr[indexPath.section][indexPath.row];
                NSString *key = self.titleKeysArr[indexPath.section][indexPath.row];
                cell.contentText = [self.model valueForKey:key];
            }
        }

    }
    
    if ((indexPath.section > [self.titlesArr indexOfObject:@"局部状况"]) | ([self.titlesArr indexOfObject:@"全身状况"] < indexPath.section && indexPath.section < [self.titlesArr indexOfObject:@"局部状况"])) {
        cell.contentView.backgroundColor = kAppBg;
    }else {
        
        cell.contentView.backgroundColor = [UIColor whiteColor];
    
    }
    return cell;

}

- (void)setTextWithIndex:(NSIndexPath *)indexPath text:(NSString *)text {

    
    if ([self.titlesArr[indexPath.section] isKindOfClass:[NSString class]]) {
        NSString *key = self.titleKeysArr[indexPath.section];
        [_model setValue:text forKey:key];
        
    }else {
        NSString *key = self.titleKeysArr[indexPath.section][indexPath.row];
        [_model setValue:text forKey:key];
        
    }
    [self.tableView reloadData];
}


#pragma mark -UITableViewDelegate------

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    defineWeakSelf;
    CGFloat height = [tableView fd_heightForCellWithIdentifier:cellID configuration:^(WCNPICCPreserveRecordAddCell * cell) {
        if ([weakSelf.titlesArr[indexPath.section] isKindOfClass:[NSString class]]) {
            cell.titleLabel.text = weakSelf.titlesArr[indexPath.section];
            NSString *key = weakSelf.titleKeysArr[indexPath.section];
            if (![key isEqualToString:@"bodyCondition"]) {
                
                cell.contentText = [self.model valueForKey:key];
            }

        }else {
            
            cell.titleLabel.text = weakSelf.titlesArr[indexPath.section][indexPath.row];
            NSString *key = weakSelf.titleKeysArr[indexPath.section][indexPath.row];
            cell.contentText = [self.model valueForKey:key];

        }
    }];
    if (height < 46) {
        height = 46;
    }
    return height;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


    defineWeakSelf;
    if (self.recordType == WCNPICCPreserveRecordTypeAdd | self.recordType == WCNPICCPreserveRecordTypeEdit) {
        
        NSString *leftTitle;
        if ([self.titlesArr[indexPath.section] isKindOfClass:[NSString class]]) {
            leftTitle = self.titlesArr[indexPath.section];
        }else {
            leftTitle = self.titlesArr[indexPath.section][indexPath.row];
            
        }
        if ([leftTitle isEqualToString:@"全身状况"]){
            
            return;
            
        }
        
        if (indexPath.section == 0) {
            WCDatePickerView *datePicker = [[WCDatePickerView alloc] init];
            datePicker.dateLabel.text = self.titlesArr[indexPath.section];
            datePicker.datePicker.maximumDate = [NSDate date];
            datePicker.close = ^(NSDate *date){
                
                [weakSelf setTextWithIndex:indexPath text:[date stringWithFormat:@"yyyy-MM-dd"]];
            };
            [datePicker show:self.navigationController completion:nil];
        }else if ([leftTitle isEqualToString:@"发热"]){
        
            WCInfoEditView *editView = [[WCInfoEditView alloc] init];
            editView.titleLabel.text = @"编辑体温";
            editView.limitLenth = 4;
            editView.type = WCInfoEditTypeMoney;
            if ([self.titlesArr[indexPath.section] isKindOfClass:[NSString class]]) {
                NSString *key = self.titleKeysArr[indexPath.section];
//                editView.textField.text = [NSString stringWithFormat:@"%@"] [self.model valueForKey:key];
            
                WSJSetTxt(editView.textField, [self.model valueForKey:key], @"");
                
            }else {
                NSString *key = self.titleKeysArr[indexPath.section][indexPath.row];
                editView.textField.text = [self.model valueForKey:key];
            }
//            [NSString stringWithFormat:@"%@%@",];
            editView.save = ^(NSString *txt){
                if (txt) {
                    [weakSelf setTextWithIndex:indexPath text:txt];
                    //[weakSelf.tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationMiddle];
                }
                return YES;
                
            };
            [editView showIn:self.navigationController.view animated:YES];
        
        
        }else if ([leftTitle isEqualToString:@"导管滑出"]){
            
            WCInfoEditView *editView = [[WCInfoEditView alloc] init];
            editView.titleLabel.text = @"滑出距离";
            editView.limitLenth = 4;
            editView.type = WCInfoEditTypeMoney;
            if ([self.titlesArr[indexPath.section] isKindOfClass:[NSString class]]) {
                NSString *key = self.titleKeysArr[indexPath.section];
                WSJSetTxt(editView.textField, [self.model valueForKey:key], @"");
                
            }else {
                NSString *key = self.titleKeysArr[indexPath.section][indexPath.row];
                WSJSetTxt(editView.textField, [self.model valueForKey:key], @"");
            }
            editView.save = ^(NSString *txt){
                if (txt) {
                    [weakSelf setTextWithIndex:indexPath text:txt];
                    //[weakSelf.tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationMiddle];
                }
                return YES;
                
            };
            [editView showIn:self.navigationController.view animated:YES];
            
            
        }else if ([leftTitle isEqualToString:@"导管破损"]){
            
            WCInfoEditView *editView = [[WCInfoEditView alloc] init];
            editView.titleLabel.text = @"破损位置";
            editView.type = WCInfoEditTypeText;
            if ([self.titlesArr[indexPath.section] isKindOfClass:[NSString class]]) {
                NSString *key = self.titleKeysArr[indexPath.section];
                editView.textField.text = [self.model valueForKey:key];
                
            }else {
                NSString *key = self.titleKeysArr[indexPath.section][indexPath.row];
                editView.textField.text = [self.model valueForKey:key];
            }
            editView.save = ^(NSString *txt){
                if (txt) {
                    [weakSelf setTextWithIndex:indexPath text:txt];
                    //[weakSelf.tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationMiddle];
                }
                return YES;
                
            };
            [editView showIn:self.navigationController.view animated:YES];
            
            
        }else if (([leftTitle isEqualToString:@"处理方式"] && indexPath.section == [self.titlesArr indexOfObject:@"穿刺侧手臂肿胀"]+1) | ([leftTitle isEqualToString:@"用药情况"])){
            
            WCNMulTextEditVC *textEditVC = [[WCNMulTextEditVC alloc] initWithType:WCNMulTextEditSave];
            if ([weakSelf.titlesArr[indexPath.section] isKindOfClass:[NSString class]]) {
                textEditVC.title = weakSelf.titlesArr[indexPath.section];
                
            }else {
                textEditVC.title = weakSelf.titlesArr[indexPath.section][indexPath.row];
                
            }
            if ([self.titlesArr[indexPath.section] isKindOfClass:[NSString class]]) {
                NSString *key = self.titleKeysArr[indexPath.section];
                textEditVC.textViewValue = [self.model valueForKey:key];
                
            }else {
                NSString *key = self.titleKeysArr[indexPath.section][indexPath.row];
                textEditVC.textViewValue = [self.model valueForKey:key];
            }
            [self.navigationController pushViewController:textEditVC animated:YES];
            textEditVC.callback = ^(NSString *text){
                
                [weakSelf setTextWithIndex:indexPath text:text];
                
            };
            
        }else {
        
            if (indexPath.row == 0) {
                WSJPickAndCustomSegment *pickVC = [[WSJPickAndCustomSegment alloc] init];
                
                if ([leftTitle isEqualToString:@"冲洗用液"]) {
                   pickVC.dataSourceArray = @[@"生理盐水", @"肝素稀释液"];
                }else if ([leftTitle isEqualToString:@"更换接头"]){
                    pickVC.dataSourceArray = @[@"正压接头", @"非正压接头"];
                
                }else if ([leftTitle isEqualToString:@"非正压接头类型"]){
                    
                    pickVC.dataSourceArray = @[@"肝素帽", @"无针接头"];
                    
                }else if ([leftTitle isEqualToString:@"局部状况"]){
                    
                    pickVC.dataSourceArray = @[@"正常", @"异常"];
                    pickVC.type = 1;
                }else if ([leftTitle isEqualToString:@"穿刺点"]){
                    pickVC.dataSourceArray = @[@"红肿", @"脓性分泌物", @"渗液"];
                    pickVC.type = 1;
                }else if ([leftTitle isEqualToString:@"穿刺侧手臂肿胀"]){
                    // 血栓、机械性静脉炎、感染
                    pickVC.dataSourceArray = @[@"血栓", @"机械性静脉炎", @"感染"];
                }else if ([leftTitle isEqualToString:@"皮肤问题"]){
                    // 渗出、皮疹、瘙痒、水疱
                    pickVC.dataSourceArray = @[@"渗出", @"皮疹", @"水疱"];
                }else if ([leftTitle isEqualToString:@"是否堵管"]){
                    // 渗出、皮疹、瘙痒、水疱
                    pickVC.dataSourceArray = @[@"堵管", @"未堵"];
                    pickVC.type = 1;
                }else {
                
                    pickVC.dataSourceArray = @[@"是", @"否"];
                }
                pickVC.callBack = ^(NSString *text, NSInteger item){
    
                if ([leftTitle isEqualToString:@"冲封管"]) {
                  
                    if (item == 0) {
                        if (![weakSelf.titleKeysArr containsObject:@"washingLiquid"]) {
                            
                            [weakSelf.titlesArr insertObject:@"冲洗用液" atIndex:indexPath.section + 1];
                            [weakSelf.titleKeysArr insertObject:@"washingLiquid" atIndex:indexPath.section + 1];
                
                        }
                    }else {
                        
                        if ([weakSelf.titleKeysArr containsObject:@"washingLiquid"]) {
                            
                            [weakSelf.titlesArr removeObject:@"冲洗用液"];
                            [weakSelf.titleKeysArr removeObject:@"washingLiquid"];
                            [weakSelf.model setValue:nil forKey:@"washingLiquid"];
                        }
                    }

                }else if ([leftTitle isEqualToString:@"更换接头"]){
                    if (item == 1) {
                        if (![weakSelf.titleKeysArr containsObject:@"nonPressureJointType"]) {
                            
                            [weakSelf.titlesArr insertObject:@"非正压接头类型" atIndex:indexPath.section + 1];
                            [weakSelf.titleKeysArr insertObject:@"nonPressureJointType" atIndex:indexPath.section + 1];
                           
                        }
                    }else {
                        if (![weakSelf.titleKeysArr containsObject:@"washingLiquid"]) {
                            
                            [weakSelf.titlesArr removeObject:@"非正压接头类型"];
                            [weakSelf.titleKeysArr removeObject:@"nonPressureJointType"];
                            [weakSelf.model setValue:nil forKey:@"nonPressureJointType"];
                           
                        }
                    }
                    

                }else if ([leftTitle isEqualToString:@"局部状况"]){
                    if ( item == 1) {
                        if (!weakSelf.isDisplayLastTitles) {
                            
                            weakSelf.isDisplayLastTitles = YES;
                            [weakSelf.titlesArr addObjectsFromArray:weakSelf.lastTitlesArr];
                            [weakSelf.titleKeysArr addObjectsFromArray:weakSelf.lastTitleKeys];
                        }
                    }else {
                        if (weakSelf.isDisplayLastTitles) {
                            weakSelf.isDisplayLastTitles = NO;
                            [weakSelf.titlesArr removeObjectsInArray:weakSelf.lastTitlesArr];
                            [weakSelf.titleKeysArr removeObjectsInArray:weakSelf.lastTitleKeys];
                            for (id obj in weakSelf.lastTitleKeys) {
                                if ([obj isKindOfClass:[NSString class]]) {
                                    [weakSelf.model setValue:nil forKey:obj];
                                }else {
                                    for (NSString *key in obj) {
                                        [weakSelf.model setValue:nil forKey:key];
                                    }
                                }
                            }
                        }
                    }
                    
                }else if ([leftTitle isEqualToString:@"穿刺侧手臂肿胀"]){
                    // 血栓、机械性静脉炎、感染
                    
                    switch (item) {
                        case 0:
                        {
                            if ([weakSelf.titleKeysArr containsObject:@"punctureArmSwellingMessure"] | [weakSelf.titleKeysArr containsObject:@"medicationSituation"]) {
                                [weakSelf.model setValue:nil forKey:weakSelf.titleKeysArr[indexPath.section +1]];
                                [weakSelf.titlesArr removeObjectAtIndex:indexPath.section +1];
                                [weakSelf.titleKeysArr removeObjectAtIndex:indexPath.section +1];
   
                            }
                        
                            if (![weakSelf.titleKeysArr containsObject:@"isBUltrasoundExamination"]) {

                                [weakSelf.titlesArr insertObject:@"是否做过B超检查" atIndex:indexPath.section + 1];
                                [weakSelf.titleKeysArr insertObject:@"isBUltrasoundExamination" atIndex:indexPath.section + 1];
                                
                            }
                        }
                            break;
                        case 1:
                        {
                            if ([weakSelf.titleKeysArr containsObject:@"isBUltrasoundExamination"] | [weakSelf.titleKeysArr containsObject:@"medicationSituation"]) {
                                [weakSelf.model setValue:nil forKey:weakSelf.titleKeysArr[indexPath.section +1]];
                                [weakSelf.titlesArr removeObjectAtIndex:indexPath.section +1];
                                [weakSelf.titleKeysArr removeObjectAtIndex:indexPath.section +1];
                                
                            }
                            
                            if (![weakSelf.titleKeysArr containsObject:@"punctureArmSwellingMessure"]) {
                                                                [weakSelf.titlesArr insertObject:@"处理方式" atIndex:indexPath.section + 1];
                                [weakSelf.titleKeysArr insertObject:@"punctureArmSwellingMessure" atIndex:indexPath.section + 1];
                                
                            }
                        }
                            break;
                        case 2:
                        {
                            if ([weakSelf.titleKeysArr containsObject:@"punctureArmSwellingMessure"] | [weakSelf.titleKeysArr containsObject:@"isBUltrasoundExamination"]) {
                                [weakSelf.model setValue:nil forKey:weakSelf.titleKeysArr[indexPath.section +1]];
                                [weakSelf.titlesArr removeObjectAtIndex:indexPath.section +1];
                                [weakSelf.titleKeysArr removeObjectAtIndex:indexPath.section +1];
                                
                            }
                            
                            if (![weakSelf.titleKeysArr containsObject:@"medicationSituation"]) {
                           
                                [weakSelf.titlesArr insertObject:@"用药情况" atIndex:indexPath.section + 1];
                                [weakSelf.titleKeysArr insertObject:@"medicationSituation" atIndex:indexPath.section + 1];
                                
                            }
                            
                        }
                            break;
                            
                        default:{
                        
                            [weakSelf.titlesArr removeObjectAtIndex:indexPath.section +1];
                            [weakSelf.titleKeysArr removeObjectAtIndex:indexPath.section +1];
                        }
                            break;
                    }
                   
                }
                    [weakSelf setTextWithIndex:indexPath text:text];
                    //[tableView reloadData];
                    
                };
                [self presentViewController:pickVC animated:YES completion:nil];
            }else {
                
                WCNMulTextEditVC *textEditVC = [[WCNMulTextEditVC alloc] initWithType:WCNMulTextEditSave];
                if ([weakSelf.titlesArr[indexPath.section] isKindOfClass:[NSString class]]) {
                    textEditVC.title = weakSelf.titlesArr[indexPath.section];
                    
                }else {
                    textEditVC.title = weakSelf.titlesArr[indexPath.section][indexPath.row];
                    
                }
                if ([self.titlesArr[indexPath.section] isKindOfClass:[NSString class]]) {
                    NSString *key = self.titleKeysArr[indexPath.section];
                    textEditVC.textViewValue = [self.model valueForKey:key];
                    
                }else {
                    NSString *key = self.titleKeysArr[indexPath.section][indexPath.row];
                    textEditVC.textViewValue = [self.model valueForKey:key];
                }
                
                [self.navigationController pushViewController:textEditVC animated:YES];
                textEditVC.callback = ^(NSString *text){
                    
                    [weakSelf setTextWithIndex:indexPath text:text];
                    
                };
            }
            
        }
        
    }
   
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsMake(0, -10, 0, 10)];
    }
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsMake(0, -10, 0, 10)];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 加载底部图片编辑
-(void)getImageWithUrls:(NSArray *)urls
{
    _webImages = [NSMutableArray array];
    NSString *path =[[NSBundle mainBundle] pathForResource:@"config" ofType:@"plist"];
    NSDictionary *plistDict =[NSDictionary dictionaryWithContentsOfFile:path];
    NSString *priod = plistDict[@"MODEL"];
    NSString *bathUrl =[plistDict[@"MEDIAS"] objectForKey:priod];
    if (urls.count == 0) {
        
        UIView *tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 130)];
        [self loadFooterView:tableFooterView];
        self.tableView.tableFooterView = tableFooterView;
        return;
    }
    [urls enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (!*stop) {
            
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                NSString *newStr =[NSString stringWithFormat:@"%@%@%@",bathUrl,@"/image/simg/",obj];
                NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:newStr]];
                UIImage *image = [UIImage imageWithData:data];
                WSJImage *wimage =[WSJImage new];
                wimage.bigImage = image;
                if (image) {
                    [_webImages addObject:wimage];
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (_webImages.count == urls.count) {
                        UIView *tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 130)];
                        [self loadFooterView:tableFooterView];
                        self.tableView.tableFooterView = tableFooterView;
                    }
                });
            });
            
        }
    }];
    
}

- (void)loadFooterView:(UIView *)tableFooterView
{
    
    WCDrawBorderView *view1 = [[WCDrawBorderView alloc] initWithFrame:CGRectMake(0, -kOnePX, kScreenWidth, 45+kOnePX)];
    view1.topBorder = YES;
    view1.borderColor = kBorderColor;
    view1.backgroundColor = [UIColor whiteColor];
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(14, 10, 250, 25)];
    label1.font = [UIFont systemFontOfSize:16.0];
    label1.text = @"记录图片（最多上传3张～）";
    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth-11-13, 16, 13, 13)];
    imageView1.image = [UIImage imageNamed:@"Arrow_Down"];
    [view1 addSubview:label1];
    [view1 addSubview:imageView1];
    
    
    WCNCareRecordDetailFooterView *footerView = [[WCNCareRecordDetailFooterView alloc] initWithFrame:CGRectMake(0, 40, kScreenWidth, 90)];
    //    footerView.backgroundColor = [UIColor greenColor];
    //NSArray *images = self.data[@"images"];
    
    WSJHorTableView *picTableView =[[WSJHorTableView alloc] initWithFrame:CGRectMake(0, 10, kScreenWidth, 65)];
    self.picTableView = picTableView;
    picTableView.maxImageCount = 3;
    picTableView.dateArray = [self.webImages mutableCopy];
    if (self.recordType == WCNPICCPreserveRecordTypeScan) {
        picTableView.type = 1;
        picTableView.imageUrls = self.imageUrls;
        
    }else {
        
        picTableView.type = 0;
    }
    
    defineWeakSelf;
    picTableView.presentDelegate = ^(){return weakSelf;};
    picTableView.blockBackImageNames = ^(NSArray *imageNames){
        [weakSelf upLoadPreserveRecordWithImageNames:imageNames];
    };
    [footerView addSubview:picTableView];
//    self.footerView = footerView;
    
    [tableFooterView addSubview:view1];
    [tableFooterView addSubview:footerView];
}

//上传拔管记录/修改拔管记录
- (void)upLoadImages{
    for (NSString *key in @[@"maintainDate",@"replacementFilm",@"flushPipe", @"replacementJoint", @"localCondition"]) {
        if (![self.model valueForKey:key]) {
            NSString *title = self.titlesArr[[self.titleKeysArr indexOfObject:key]];
            NSString *message = [title stringByAppendingFormat:@"不能为空～"];
            WSJSHOW(message);
            return;
        }
    }
    
    if (_isDisplayLastTitles) {
        if (![self isWriteMoreOneLocalCondition]) {
            WSJSHOW(@"必须填写至少一个异常内容～");
            return;
        }
    }
    self.saveBtn.enabled = NO;
    [self.picTableView startUploadImages];
}

@end
